"use strict"; // 表示当前js文件运行使用严格模式
const path = require("path"); // node.js的path模块
// 导入配置文件
const defaultSettings = require("./src/settings.js");
// 将相对路径处理成绝对路径
function resolve(dir) {
  return path.join(__dirname, dir);
}
// 网页标题
const name = defaultSettings.title || "vue Admin Template"; // page title

// If your port is set to 80,
// 运行端口号，默认读取环境变量里面的prot；如果没有读取npm运行；如果还没有就用9528
const port = process.env.port || process.env.npm_config_port || 9528; // dev port

// 脚手架配置
//  https://cli.vuejs.org/config/
module.exports = {
  publicPath: "/", // 运行路径, index.html里面的文件依赖路径关系
  outputDir: "dist", // 打包输出目录
  assetsDir: "static", // 打包之后静态文件存储目录
  lintOnSave: process.env.NODE_ENV === "development", // 是否使用eslint
  productionSourceMap: false, // 是否打包关闭map文件产出
  // 开发服务器设置
  devServer: {
    port: port, // 运行端口
    open: true, // 是否自动打开浏览器
    overlay: {
      warnings: false,
      errors: true,
    },
    proxy: {
      // #region
      // // 假设：   要去请求   "http://www.baidu.com/user/login"
      // // 8888端口 前端代码(你)中发生请求写成   /api/user/login  ==>找8888的服务器(父)要内容  【没有跨域】
      // // 8888后端服务器(父)  找  http://www.baidu.com/user/login(叔)  【服务器对服务器要数据，没有跨域】
      // "/api": {
      //   target: "http://www.baidu.com", // 代理地址
      //   changeOrigin: true, // 是否跨域
      //   pathRewrite: {
      //     // 重写路径
      //     "^/api": "",
      //   },
      // },
      // #endregion
      "/api": {
        // target: "http://ihrm-java.itheima.net/", // 跨域地址
        target: "http://ihrm.itheima.net/", // 跨域地址
        changeOrigin: true, // 要跨域
        // 路径不需要重写：因为接口都在 http://ihrm-java.itheima.net/api 下面
      },
    },
    // 导入mockjs的服务器
    // before: require('./mock/mock-server.js')
  },
  // webpack的对象式配置
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
  },
  // webpack的函数式配置
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin("preload").tap(() => [
      {
        rel: "preload",
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: "initial",
      },
    ]);

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete("prefetch");

    // set svg-sprite-loader
    config.module.rule("svg").exclude.add(resolve("src/icons")).end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();

    config.when(process.env.NODE_ENV !== "development", (config) => {
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-webpack-plugin", [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/,
          },
        ])
        .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial", // only package third parties that are initially dependent
          },
          elementUI: {
            name: "chunk-elementUI", // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      });
      // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk("single");
    });
  },
};
